using System.Collections.Generic;
using UFrame.MapJson;
using UnityEditor;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// MonsterEditor
    /// </summary>
    public class MonsterEditor : MapEditorBase
    {
        /// <summary>
        /// 此类的一个全局对象
        /// </summary>
        private static MonsterEditor windows = null;

        /// <summary>
        /// 滚动位置
        /// </summary>
        public Vector2 rollPosition = Vector2.zero;

        /// <summary>
        /// Monster数据
        /// </summary>
        List<MonsterInfo> data = new List<MonsterInfo>();

        /// <summary>
        /// 移出表数据索引
        /// </summary>
        List<int> index = new List<int>();

        SceneView.OnSceneFunc m_delegate;

        /// <summary>
        /// 打开
        /// </summary>
        public static void OpenWindow()
        {
            if (windows == null)
            {
                windows = MapEditorBase.GetWindowWithRect<MonsterEditor>(new Rect(0, 0, 750, 400), false, "Monster", true);
                windows.InitWindow();
                windows.Show();
            }
        }

        public MonsterEditor()
        {
            m_delegate = new SceneView.OnSceneFunc(OnSceneFunc);
            SceneView.onSceneGUIDelegate += m_delegate;
        }

        /// <summary>
        /// 初始化窗口
        /// </summary>
        private void InitWindow()
        {
            /*			XmlConfig.MonsterXmlConfig mxc = DataManagerInstance.instance.Get<XmlConfig.MonsterXmlConfig>();
                        if (mxc != null)
                        {
                            this.data = mxc.data;
                            for (int i = 0; i < data.Count; ++i)
                            {
                                data[i].go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                                data[i].go.name = data[i].id.ToString();
                                data[i].go.transform.localPosition = data[i].position;
                            }
                        }

                        index = new List<int>();*/
        }

        /// <summary>
        /// 窗口关闭时调用
        /// </summary>
        public void OnDestroy()
        {
            Destroy();
        }

        void Destroy()
        {
            if (data != null)
            {
                for (int i = 0; i < data.Count; ++i)
                {
                    GameObject.DestroyImmediate(data[i].go);
                }

            }

            index.Clear();
            SceneView.onSceneGUIDelegate -= m_delegate;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        public static void CloseWindow()
        {
            if (windows)
            {
                //销毁对象
                windows.Destroy();
                //关闭窗口
                windows.Close();
                windows = null;
            }
        }

        /// <summary>
        /// GUI
        /// </summary>
        void OnGUI()
        {
            GUI.backgroundColor = Color.gray;
            GUILayout.BeginArea(new Rect(position.width * 0.01f, position.height * 0.02f, position.width * 0.98f, position.height * 0.74f));
            rollPosition = GUILayout.BeginScrollView(rollPosition);

            index.Clear();
            for (int i = 0; i < data.Count; ++i)
            {
                GUILayout.BeginHorizontal();
                GUI.color = Color.white;
                GUILayout.Label("MonsterId :");
                int id = EditorGUILayout.IntField(data[i].id);
                if (id != data[i].id)
                {
                    data[i].id = id;
                    data[i].go.name = id.ToString();
                }

                GUILayout.Label("EventId :");
                int eventid = EditorGUILayout.IntField(data[i].eventid);
                if (eventid != data[i].eventid)
                {
                    data[i].eventid = eventid;
                }

                GUILayout.Label("Radius :");
                data[i].radius = EditorGUILayout.FloatField(data[i].radius);
                GUILayout.Label("Number :");
                data[i].number = EditorGUILayout.IntField(data[i].number);
                GUILayout.Label("Rotation :");
                data[i].rotation = EditorGUILayout.FloatField(data[i].rotation);
                GUI.color = Color.green;
                if (GUILayout.Button("select", GUILayout.Width(64)))
                {
                    Selection.activeGameObject = data[i].go;
                }

                GUI.color = Color.red;
                if (GUILayout.Button("delete", GUILayout.Width(64)))
                {
                    index.Add(i);
                }

                GUILayout.EndHorizontal();
            }

            GUILayout.EndScrollView();
            GUILayout.EndArea();

            for (int i = 0; i < index.Count; ++i)
            {
                DestroyImmediate(data[index[i]].go);
                data.RemoveAt(index[i]);
            }

            GUI.color = Color.green;
            if (GUI.Button(new Rect(position.width * 0.01f, position.height * 0.78f, position.width * 0.98f, position.height * 0.07f), "+"))
            {
                MonsterInfo monster = new MonsterInfo();
                monster.go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                Vector3 dir = SceneView.lastActiveSceneView.rotation * Vector3.forward;

                monster.go.transform.position = SceneView.lastActiveSceneView.pivot + dir * 2;
                monster.go.name = monster.id.ToString();
                data.Add(monster);
            }
            if (GUI.Button(new Rect(position.width * 0.01f, position.height * 0.86f, position.width * 0.98f, position.height * 0.12f), "保存"))
            {
                Save();
            }
            GUI.color = Color.gray;
        }



        /// <summary>
        /// 保存数据
        /// </summary>
        public override void Save()
        {

            for (int i = 0; i < data.Count; ++i)
            {
                data[i].position = data[i].go.transform.localPosition;
            }
            isChange = false;

        }

        void OnSceneFunc(SceneView sceneView)
        {
            try
            {
                Handles.color = new Color(1f, 0f, 0f, 0.3f);
                for (int i = 0; i < data.Count; ++i)
                {
                    Handles.DrawSolidDisc(data[i].go.transform.localPosition, Vector3.up, data[i].radius);
                }
            }
            catch
            {
                Destroy();
            }
        }


    }
}